package com.chinasofti.field;

import java.lang.reflect.Field;

public class TestFieldDemo {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        //method1();
        //method2();
        //method3();
        Class clazz = Class.forName("com.chinasofti.ref.field.Student");
        Field field = clazz.getDeclaredField("money");//返回单个成员变量
        System.out.println("field = " + field);
    }

    private static void method3() throws ClassNotFoundException, NoSuchFieldException {
        Class clazz = Class.forName("com.chinasofti.ref.field.Student");
        Field field = clazz.getField("name");//获取公开单独的属性
        //Field field = clazz.getField("name1");//NoSuchFieldException
        //Field field = clazz.getField("money");//NoSuchFieldException: money
        System.out.println("field = " + field);
    }

    private static void method2() throws ClassNotFoundException {
        Class clazz = Class.forName("com.chinasofti.ref.field.Student");
        Field[] fields = clazz.getDeclaredFields();//获取所有成员变量数组
        for (Field field:
                fields) {
            System.out.println(field);
        }
    }

    private static void method1() throws ClassNotFoundException {
        Class clazz = Class.forName("com.chinasofti.ref.field.Student");

        Field[] fields = clazz.getFields();//获取公开的成员变量数组
        for (Field field:
                fields) {
            System.out.println(field);
        }
    }
}